<?php
class Response{
	
	
public function getLatestResponse($id){
	
    $sql_get_response_id = "SELECT MAX(`id`)FROM `response` WHERE `student_id` = '$id'";
    $rid = mysql_query($sql_get_response_id)or die(mysql_error());
    $result_get_response_id = mysql_fetch_row($rid);
    foreach($result_get_response_id as $key=>$value){
    $response_id = $value;
    }
    
    if($response_id){
    	return $response_id;
    }else{
    	return false;
    }
}	
	

public function saveResponse($data){
	$sql = "INSERT INTO `response` (`survey_id`,`student_id`,`student_name`,`timestamp`)
	       VALUES ( '{$data['survey_id']}','{$data['student_id']}','{$data['student_name']}','{$data['timestamp']}')";
	$result = mysql_query($sql)or die ("Save Response: ". mysql_error());
	if($result){
		return true;
	}else{
		return false;
	}
}


public function saveResponseValues($data){
	$sql = "INSERT INTO `response_value` (`response_id`,`survey_id`,`question_id`,`response`,`comment`)
	       VALUES ('{$data['response_id']}','{$data['survey_id']}','{$data['question_id']}','{$data['response']}','{$data['comment']}')";
    $result = mysql_query($sql)or die("Save Response Value: ".mysql_error());
    
    if($result){
    	return true;
    }else{
    	return false;
    }

}

public function updateResponseValues($data){
	$sql = "UPDATE `response_value` SET `response`='{$data['response']}',`comment`='{$data['comment']}'
	                                    WHERE `question_id`='{$data['question_id']}' AND `response_id`='{$data['response_id']}'";
	      
    $result = mysql_query($sql)or die("Save Response Value: ".mysql_error());
    
    if($result){
    	return true;
    }else{
    	return false;
    }

}

public function checkResponse($response_id, $question_id){
	$sql = "SELECT * FROM `response_value` WHERE `question_id`='$question_id' AND `response_id`='$response_id'";
	$result = mysql_query($sql)or die("Check Question Response: ".mysql_error());
	$response = mysql_fetch_row($result);
	if($response){
		return true;
	}else{
		return false;
	}
}

public function getResponseValue($response_id,$question_id){
	$sql = "SELECT `response` FROM `response_value` WHERE `question_id`='$question_id' AND `response_id`='$response_id'";
	$result = mysql_query($sql)or die("GET Question Response: ".mysql_error());
	$response = mysql_fetch_object($result);
	if($response){
		return $response->response;
	}else{
		return false;
	}
}

public function getAllResponseValues($question_id){
	$sql = "SELECT `response` FROM `response_value` WHERE `question_id`='$question_id'";
	$result = mysql_query($sql)or die("GET Question Response: ".mysql_error());
	
	while ( $r=mysql_fetch_object($result)){
	$response[] =$r ;
	}
	if($response){
		return $response;
	}else{
		return false;
	}
}


public function getResponseCount($question_id,$response){
	$sql = "SELECT `response` FROM `response_value` WHERE `question_id`='$question_id' AND `response`='$response'";
	$result = mysql_query($sql)or die("GET Question Response count : ".mysql_error());
	$count=0;
	while($r=mysql_fetch_object($result)){
	$response = $r ;
	$count++;
	}
	//$count = sizeof($response);
	if($response){
		return $count;
	}else{
		return false;
	}
}

}


?>